1. Field of the Invention
The present invention relates to computers having a trapping function, more particular to a computer having an improved system for processing software traps.
2. Description of the Prior Art
The methods of transferring control of the programs to be processed in a computer include subroutine calls, interrupts, and traps. Traps are an interrupt request internally generated as a direct and immediate result of the detection of an exceptional condition by memory manangement, such as the execution of an illegal operation or undefined instruction, the overflow of computation results or division by zero, the execution of a breakpoint or program trace in debugging, and the execution of a software trap setting instruction. Software trap setting instructions are used to set a trap intentionally by program for calling a trap process program, which is comparable to subroutine calls. They are useful for changing the executive environment of a program, especially for calling a service program provided in the system. Examples are the INT instruction for the U.S. Intel Corporation 16-bit microprocessor iAPX 86, the TRAP instruction for the U.S. Motorola Co. 16-bit microprocessor MC 68000, and the SVC instruction for the U.S. National Semiconductor Co. 32-bit microprocessor NS 32032.
The control of a program call by a subroutine call, interrupt, or trap works well with a stack mechanism for stacking data by a last-in, first-out method. When a subroutine is called from a program in execution or control is transferred to another program by an interrupt or trap, the return address and other necessary information are stored in a stack, and the information in the stack restored upon completion of process of the subroutine or interrupt or trap. The return address is also restored to restart execution of the instruction at the address. In such a mechanism, the last stored information is first restored so that even if a program is called in nesting, control is always returned to the orignal calling program upon completion of the process of a routine or program. Most of the present computers makes such control by means of the stack mechanism.
FIG. 5 shows in block form a conventional computer having a stack mechanism to make such call control. It consists of a main memory 7, a program counter 3 for holding an address in the main memory 7 in which the instruction to be executed is stored, a stack pointer 2 for pointing to the highest location of data stacked in the main memory 7 by a last-in, first-out method, a general or special purpose register 4, a control register 5 for holding control or status flages of the computer, and a control unit 6b. The main memory 7 has a stack area 8 for stacking data by a last-in, first-out method, and the highest location of data stacked in this area is pointed to by the stack pointer 2. The control unit 6b is provided to interpret the instruction retrieved from the main memory 7 at the address indicated by the program counter 3 and control its execution and the transmission of data between the program counter 3, stack pointer 2, register 4 and control register 5, and the main memory 7.
How a trap is set in such a computer will be described. FIG. 6 shows the stacking states in the main memory 7 before and after the setting of a trap. Before the setting a trap, the stack pointer 2 holds a stack location [S0] and points to the highest location of data stacked in the stack area 8 by the previous execution of a program as shown by an arrow 9. At the setting of a trap, the necessary information containing a return address is stored in the stack area 8 between stack locations [S0+1] and [S1b], and the content of the stack pointer 2 is updated to the stack location [S1b] as shown by an arrow 10b. Then, the top address of a specific trap processing program inherent to the computer or operating system is stored in the program counter 3. As a result, the next instruction is retrieved from the main memory 7 at the address stored in this program counter 3 for execution. The information stored in the stack area 8 between the locations [S0+1]and [S1b] may be the content or return address of the program counter 3, the contents of the control register 5 and general or special purpose register 4, and specific data in the main memory 7. For example, the return address and the content of a control register are stored for the Motorola Co. MC68000.
Next, the trap return, which corresponds to the above trap setting, will be described. In a trap processing program, stack areas may be used for a certain purpose, but they must be released immediately before a trap return, and the stack pointer 2 must be in the state immediately after the setting of a trap as shown by the arrow 10b. From this state, the information stored between the stack locations [S0+1] and [S1b] is returned. This information has been saved at the setting of a trap. If the contents of a control register or general or special purpose register have been stored, they are returned. If the specific data in the main memory have been saved, the necessary one and the return address are returned. This return of the return address brings control back to the program which was executed before the setting of a trap. The stack pointer 2 is returned to the state before the setting of a trap as shown by the arrow 9. The control unit 6b1 provided to control these trap setting and return operations.
In such a computer, the stack grows dynamically every time a program or subroutine is called. For example, when a setting instruction, and the called system program S calls a subroutine B as shown in FIG. 7, the stack changes as shown in FIG. 8. The stack pointer 2, which has pointed to a stack location [SP0] as shown by a solid arrow 30 immediately before the execution of a software trap setting instruction, now points to a stack location [SP1b] when the information containing a return address is stored at the highest stack area 21b as shown by a broken arrow 31b by the operation of a trap set by execution of the software trap setting instruction. If the area 22 is used for a certain purpose in execution of the process 1 of the system program S (FIG. 7,) the stack pointer 2 is in a state such as shown by a broken arrow 32 immediately before the subroutine B is called from the system program, indicating a stack location [SP2]. Then, when the subroutine B is called, the information containing a return address is stored in the highest stack location 23, and the stack pointer 2 points to a stack location [SP3] as shown by a broken arrow 33.
Suppose that the execution of the process 3 of the subroutine (FIG. 7) is controlled with the information of the program A saved in the area 21b under a software trap setting instruction. An example is that decision on an interrupt request generated in execution of the subroutine B is made with the identification information for interrupt enable stored in the area 21a. There are two methods for processing such a case.
One of the two methods is that reference is made to the identification information from the information saved in the area 21b under a software trap setting instruction. To do this, it is necessary to store at the time of a subroutine call, trap, or interrupt a return address and the location in which the information has been stored at the time of a previous subroutine call, trap, or interrupt. When reference is made to the identification information in the area 21b from the subroutine B, the stack location indicating the area 21b must be found from the information saved in the area 23 for making reference to the desired identification information.
The other method is to transfer as a parameter the identification information used in the subroutine B once for each call. To achieve this, it is necessary to copy the identification information in the area 21b into the area 23 at the time of calling the subroutine B from the system program S. In this case, reference to the identification information from the subroutine B is made to the copy of the original identification information in the area 23.
When the number of calling stages is as small as in this example, the additional procedure for reference to the identification information is not very burdensome. However, when reference is made to the identification information from a subroutine called in many levels of nesting or recursive definition this additional procedure reduces the process efficiency to a large extent. The latter method is better than the former in terms of efficiency in reference to the identification information but requires an additional memory area for copying the identification information for every call. This waste increases with the size of the identification information or the number of nesting levels for call.
The feature of a stack that program control is always returned to the calling program can be impractical for some applications. For example, when an error trap is set from an error in conditions for calling the system program S from the program A in execution of the subroutine B, thereby calling an error processing program, even if the continuation of process of the system program S is found impossible in the error processing program, control is not immediately returned to the program A but to the subroutine B from the error processing program by a trap return as shown by an arrow 53b in FIG. 7, to the system program S from the subroutine B as shown by an arrow 54, and then to the program A from the system program S by a trap return as shown by an arrow 55b in FIG. 7. Apparently, this is an excessive process. In addition, in order to avoid an "error chain reaction" by which an error brings about another error, it is necessary to carry out another error decision process. Such an error decision process is executed even in a normal process with no errors, thus reducing the program process efficiency.
By the conventional technology, when a program or subroutine called directly or indirectly under a software trap setting instruction is executed, it is very difficult to make reference to the information about the calling program saved in the memory at the setting of a software trap, and fine control, such as interrupt enable, becomes a complicated process. It is also difficult to make an immediate return to the calling program which has executed the software trap setting instruction from the called program or subroutine.